package com.my.socket.nio.v32;

import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Created by ZhongGang
 * at 2018/8/24 15:55
 */
public class Server {

    public static void main(String[] args) throws Exception {
        Selector selector = Selector.open();

        Selector[] subSelectors = new Selector[3];
        for (int i = 0; i < subSelectors.length; i++) {
            subSelectors[i] = Selector.open();
        }

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1", 9999));
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        new Thread(new Acceptor(selector, subSelectors), "acceptor").start();

        for (int i = 0; i < subSelectors.length; i++) {
            new Thread(new Poller(subSelectors[i]), "poller-" + i).start();
        }
    }
}
